package api

import (
	"github.com/emicklei/go-restful"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"metabank/runtime"
	"net/http"
)

const (
	GroupName = "router.metabank.org"
)

var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha1"}

func AddToContainer(container *restful.Container, cache map[string]map[string]string) error {
	ws := runtime.NewWebService(GroupVersion)
	handler := NewTenantHandler(cache)

	ws.Route(ws.GET("/routeip/{customerNumber}").
		To(handler.getrouter).
		Doc("Retrieve gateways details.").
		Param(ws.PathParameter("namespace", "the watching namespace of the gateway")).
		Returns(http.StatusOK, "ok", map[string]string{}))

	ws.Route(ws.POST("/upload").
		To(handler.upload).
		Consumes(runtime.MimeMultipartFormData, restful.MIME_JSON).
		Returns(http.StatusOK, "ok", map[string]string{})).
		Doc("Upload app install yaml file.")

	ws.Route(ws.GET("/health").
		Doc("Check service is healthy.").
		To(func(request *restful.Request, response *restful.Response) {
			response.WriteAsJson(map[string]string{"health": "ok"})
		}))

	container.Add(ws)
	return nil
}
